package com.sensePlusVR.utils;

import android.content.Context;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.UserDto;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午3:06:04
 * @desc: 用户信息操作
 *
 */
public class UserUtil {

	private static final String TAG = UserUtil.class.getSimpleName();
	
	/**
	 * 取消所有正在下载
	 */
	public static void cancelAllDownload(){
		
		GameDownload.cancelAllDownloading();
		
		
	}
	/**
	 * 清空本地数据库
	 * @param context
	 */
    public static void clearnLocalDB(Context context){
    	MyLog.d(TAG, "clearnLocalDB");
    	DbManager db = VRApplication.getDbManager();
    	try {
			db.dropDb();
		} catch (DbException e) {
			e.printStackTrace();
			MyLog.d(TAG, "clearnLocalDB failed!");
		}
    }
    
    /**
     * 清空本地保存的收藏关注IDs
     * @param context
     */
    public static void clearnFollowCollectIds(Context context){
    	MyLog.d(TAG, "clearnFollowCollectIds");
    	MySharedPreferences.saveObject(context, Constant.USER_FOLLOW_IDS, null);
    	
    }
    
    /**
     * 清除手势密码
     * @param context
     */
    public static void clearnGesturePSW(Context context){
    	MyLog.d(TAG, "clearnGesturePSW");
        MySharedPreferences.save2(context,MySharedPreferences.NAME_NOT_CLEAR,Constant.SP_INPUTCODE, "");
    }
    
    /**
     * 清除用户相关信息
     * @param context
     */
    public static void clearnUserMsg(Context context){
    	MyLog.d(TAG, "clearnUserMsg");
    	//清除token
        MySharedPreferences.save2(context,MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_TOKEN, Constant.STR_EMPTY);
        //清除用户id
        MySharedPreferences.save2(context,MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_ID, Constant.NUMBER_0);
    }
    /**
     * 清空数据
     * 
     * @param context
     */
    public static void clearnData(Context context) {
    	MyLog.d(TAG, "clearnData");
    	cancelAllDownload();
    	clearnLocalDB(context);
    	clearnFollowCollectIds(context);
    	clearnGesturePSW(context);
    	clearnUserMsg(context);
    }
    
    
    /**
     * 保存用户信息
     * 
     * @param mContext
     * @param userDto
     * @param isLocalSave
     *            是否是本地存储
     */
    public static void saveUserInfo(final Context mContext, UserDto userDto,
            boolean isLocalSave) {
        if (ObjectUtils.isEmpty(userDto)) {
            userDto = new UserDto();
        }
        // 将取得的用户信息保存
        MySharedPreferences.save2(mContext,MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_LOGINNAME,
                userDto.getLoginName());
        MySharedPreferences.save2(mContext,MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_SHOWNAME,
                userDto.getShowName());
        MySharedPreferences.save2(mContext, MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_ID, userDto.getId());
        MySharedPreferences.save2(mContext, MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_SEX, userDto.getSex());
    }
    
    /**
     * 是否是同一用户
     * 
     * @param context
     * @return
     */
//    public static boolean isSameUser(Context context) {
//        return MySharedPreferences.getBoolean2(context,MySharedPreferences.NAME_NOT_CLEAR,Constant.IS_SAME_USER);
//    }
    
    /**
     * 保存是同一用户
     * 
     * @param context
     * @param isSameUser
     */
//    public static void saveIsSameUser(Context context, boolean isSameUser) {
//        MySharedPreferences.save2(context,MySharedPreferences.NAME_NOT_CLEAR, Constant.IS_SAME_USER, isSameUser);
//    }
    
//    public static void saveLoginNameUser(Context context,String userName){
//    	  if (context == null) {
//              return;
//          }
//    	MySharedPreferences.save2(context, MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_LOGINNAME, userName);
//    }
    
    /**
     * 获取当前登录用户的登录名
     */
    public static String getLoginNameUser(Context mContext) {
        if (mContext == null) {
            return Constant.STR_EMPTY;
        }
        return MySharedPreferences.getString2(mContext, MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_LOGINNAME);
    }
    
    /**
     * 
     * @param context
     * @return
     * 判断用户有没有登录过，若果没有
     */
    public static boolean isUserLogined(Context context){
		String nowToken = MySharedPreferences.getString2(context,MySharedPreferences.NAME_NOT_CLEAR,
				Constant.LOGIN_SUCCESS_TOKEN);
		String nowExpires = MySharedPreferences.getString2(context,
				MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_EXPIRES);

		MyLog.d(TAG, "Token : " + nowToken);
		MyLog.d(TAG, "Expires :" + nowExpires);
		// 判断用户是否登陆过
		if (nowToken == null || nowToken.length() < Constant.NUMBER_1) {
			return false;
			/*MyPageJump.pageJump(context,
					LoginActivity.class);
			((BaseActivity)context).finish();*/
		} else {
			//checkServer(context);
			return true;
		}
    }
    
    public static int getUserId(){
    	return MySharedPreferences.getInteger2(VRApplication.getAppContext(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_ID);
    }
    
    public static String getUserToken(){
    	return MySharedPreferences.getString2(VRApplication.getAppContext(), MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_TOKEN);
    }
    
	/**
	 * 携带Token登陆
	 */
//	private static void checkServer(Context context) {
//		int id = MySharedPreferences.getInteger2(context,MySharedPreferences.NAME_NOT_CLEAR,
//				Constant.LOGIN_SUCCESS_ID);
//		String userName = MySharedPreferences.getString2(context,MySharedPreferences.NAME_NOT_CLEAR,
//				Constant.USERNAME);
//		String password = MySharedPreferences.getString2(context,MySharedPreferences.NAME_NOT_CLEAR,
//				Constant.PASSWORD);
//		NetRequestManager.startLoginRequestResult(context,
//				userName, password);
//		
//	}
}
